﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Web.Http;

using AttributeRouting;
using AttributeRouting.Web.Mvc;
using Newtonsoft.Json.Linq;
using PH.Model.Results;
using PH.Model.Accounts;
using PH.Logic.Accounts;
namespace PH.Web.Management.Areas.Accounts.Controllers
{
    [RouteArea("Accounts")]
    [RoutePrefix("User")]
    public class UserApiController : ApiController
    {
        [Route("UserLogin")]
        [HttpPost]
        public JsonResult<bool> Login(JObject request)
        {
            var errinfo = "登陆失败，原因：";
            try
            {
                var userName = request.Value<string>("userName");
                var password = request.Value<string>("password");
                if (string.IsNullOrEmpty(userName) || string.IsNullOrEmpty(password)) throw new Utils.PhException("请输入用户名和密码");

                var user = UserLogic.Login(userName, password);
                if (user != null)
                {
                    System.Web.Security.FormsAuthentication.SetAuthCookie(user.Guid.ToString(), true);
                    PH.Web.Global.WebContext.CurrentUser = user;
                    //PH.Web.Global.WebContext.CurrentSchool = user.School;
                    return new JsonResult<bool>(true);
                }
            }
            catch (Exception ex)
            {
                errinfo = ex.Message;
                Common.Logger.Error("", ex);
            }
            return new JsonResult<bool>(false, errinfo, false);
        }

        [Route("ChangePassword")]
        [HttpPost]
        public JsonResult<bool> ChangePassword(JObject request)
        {
            var errinfo = "操作失败。";
            try
            {
                var userName = request.Value<string>("userName");
                var password = request.Value<string>("password");
                var newPassword = request.Value<string>("newPassword");
                if (UserLogic.ChangePassword(userName, password, newPassword))
                    return new JsonResult<bool>(true);
            }
            catch (Exception ex)
            {
                errinfo = ex.Message;
                Common.Logger.Error("", ex);
            }
            return new JsonResult<bool>(false, errinfo, false);
        }

        [Route("SaveUser")]
        [HttpPost]
        public JsonResult<bool> SaveUser(JObject request)
        {
            var errinfo = "操作失败。";
            try
            {
                var user = new UserModel();
                var guid = request.Value<string>("guid");
                if (!string.IsNullOrEmpty(guid))
                    user.Guid = new Guid(guid);
                user.UserName = request.Value<string>("userName");
                user.RealName = request.Value<string>("realname");
                user.Mobile = request.Value<string>("mobile");
                user.Email = request.Value<string>("email");
                
                UserLogic.UpdateUser(user);
                return new JsonResult<bool>(true);
            }
            catch (Exception ex)
            {
                errinfo = ex.Message;
                Common.Logger.Error("", ex);
            }
            return new JsonResult<bool>(false, errinfo, false);
        }

        [Route("SendPasswordMail")]
        [HttpPost]
        public JsonResult<bool> SendPasswordMail(JObject request)
        {
            var errinfo = "操作失败。";
            try
            {
                var email = request.Value<string>("email");
                var user = UserLogic.GetUserByEmail(email);
                if (user != null)
                {
                    var toEmail=new System.Net.Mail.MailAddressCollection();
                    toEmail.Add(user.Email);
                    var htmlContent = string.Format("亲爱的：{0} 您好，请点击以下链接重置您的密码。请尽快修改您的密码，此链接将在五个小时后过期。<br /> <a href='http://www.wzxsjkw.com/Accounts/User/ResetPassword/{1}' >http://www.wzxsjkw.com/Accounts/User/ResetPassword/{1}</a>", user.RealName, user.Guid);
                    Utils.Email.SendEmail("温州学生体质健康网--重置密码", htmlContent, new System.Net.Mail.MailAddress("wzxsjkw@163.com", "温州学生体质健康网"), toEmail);
                    UserLogic.RequestPasswordModify(user.Guid);
                    return new JsonResult<bool>(true);
                }
                else
                    throw new Utils.PhException("没有找到此用户，请检查Email是否正确。");
            }
            catch (Utils.PhException ex) { errinfo = ex.Message; }
            catch (Exception ex)
            {
                errinfo = ex.Message;
                Common.Logger.Error("", ex);
            }
            return new JsonResult<bool>(false, errinfo, false);
        }

        [Route("ResetPassword")]
        [HttpPost]
        public JsonResult<bool> ResetPassword(JObject request)
        {
            var errinfo = "操作失败。";
            try
            {
                var guid = request.Value<string>("guid");
                if (!string.IsNullOrEmpty(guid))
                {
                    var password = request.Value<string>("password");
                    var ret = UserLogic.ResetPassword(new Guid(guid), password);
                    return new JsonResult<bool>(true);
                }
            }
            catch (Utils.PhException ex) { errinfo = ex.Message; }
            catch (Exception ex)
            {
                errinfo = ex.Message;
                Common.Logger.Error("", ex);
            }
            return new JsonResult<bool>(false, errinfo, false);
        }

        [Route("ChangeEmail")]
        [HttpPost]
        [Authorize(Roles="Bureau")]
        public JsonResult<bool> ChangeEmail(JObject request)
        {
            var errinfo = "操作失败。";
            try
            {
                var sguid = request.Value<string>("guid");
                if (!string.IsNullOrEmpty(sguid))
                {
                    var guid = new Guid(sguid);
                    var email = request.Value<string>("email");
                    UserLogic.ChangeEmail(guid, email);
                    return new JsonResult<bool>(true);
                }
            }
            catch (Utils.PhException ex) { errinfo = ex.Message; }
            catch (Exception ex)
            {
                errinfo = ex.Message;
                Common.Logger.Error("", ex);
            }
            return new JsonResult<bool>(false, errinfo, false);
        }
    }
}
